package com.google.code.sms.controller;

import java.io.File;

import com.google.code.sms.domain.MediaElement;
import com.google.code.sms.service.MediaElementService;
import com.google.code.sms.service.PlayerService;
import com.google.code.sms.service.SettingsService;
import java.awt.Dimension;
import java.util.ArrayList;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller for the page used to play videos.
 *
 * @author Scott Ware
 */
public class VideoPlayerController extends ParameterizableViewController {

    public static final int DEFAULT_QUALITY = 360;
    
    private MediaElementService mediaElementService;
    private PlayerService playerService;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, Object> map = new HashMap<>();
        String url = request.getRequestURL().toString();
        int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
        MediaElement mediaElement = mediaElementService.getMediaElement(id);

        int timeOffset = ServletRequestUtils.getIntParameter(request, "timeOffset", 0);
        timeOffset = Math.max(0, timeOffset);
        Integer duration = mediaElement.getMetaData().getDuration();
        File poster = mediaElementService.getPoster(mediaElement);
        File subtitles = mediaElementService.getSubtitles(mediaElement);
        String posterURL = poster == null ? null : url.replaceFirst("/videoPlayer.view", "/coverArt.view?size=640&getPoster=true&id=" + mediaElement.getId());
        String subtitlesURL = subtitles == null ? null : url.replaceFirst("/videoPlayer.view", "/subtitles?&id=" + mediaElement.getId());
        String streamURL = url.replaceFirst("/videoPlayer.view", "/stream?id=" + mediaElement.getId() + "&player=" + playerService.getPlayer(request, response).getId());
        
        map.put("video", mediaElement);
        map.put("streamURL", streamURL);
        map.put("defaultQuality", ServletRequestUtils.getIntParameter(request, "defaultQuality", DEFAULT_QUALITY));
        map.put("duration", duration);
        map.put("timeOffset", timeOffset);
        map.put("qualities", getApplicableQualitySettings(mediaElement));
        map.put("poster", posterURL);
        map.put("subtitles", subtitlesURL);

        ModelAndView result = super.handleRequestInternal(request, response);
        result.addObject("model", map);
        return result;
    }

    public Integer[] getApplicableQualitySettings(MediaElement element)
    {
        List<Integer> qualities = new ArrayList<>();
        
        Dimension resolution = new Dimension(element.getMetaData().getWidth(), element.getMetaData().getHeight());
        
        if(resolution.getWidth() >= 426 || resolution.getHeight() >= 240)
        {
            qualities.add(240);
        }
        
        if(resolution.getWidth() >= 640 || resolution.getHeight() >= 360)
        {
            qualities.add(360);
        }
        
        if(resolution.getWidth() >= 854 || resolution.getHeight() >= 480)
        {
            qualities.add(480);
        }
        
        if(resolution.getWidth() >= 1280 || resolution.getHeight() >= 720)
        {
            qualities.add(720);
        }
        
        if(resolution.getWidth() >= 1920 || resolution.getHeight() >= 1080)
        {
            qualities.add(1080);
        }
        
        return qualities.toArray(new Integer[qualities.size()]);
    }
    
    public void setMediaElementService(MediaElementService mediaElementService) {
        this.mediaElementService = mediaElementService;
    }

    public void setPlayerService(PlayerService playerService) {
        this.playerService = playerService;
    }
}
